package com.alogic.cert.xscript;

import java.math.BigInteger;
import java.util.Stack;

import com.alogic.cert.CertificateContent;
import com.alogic.cert.CertificateStore;
import com.alogic.cert.CertificateStoreFactory;
import com.alogic.cert.PemCertificateContent;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.KeyGen;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 采用根证书签发一个子证书
 * 
 * @author yyduan
 * 
 * @since 1.6.11.9
 * @version 1.6.11.56 [20180823 duanyy] <br>
 * - 证书的序列号可定制; <br>
 *
 * @version 1.6.13.40 [20210306 duanyy] <br>
 * - 优化证书框架 <br>
 */
public class ChildCert extends NS{
	protected String cid = "$cert";
	protected String $cn;
	protected String $sn;
	
	public ChildCert(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		
		$cn = PropertiesConstants.getRaw(p,"cn","");
		$sn = PropertiesConstants.getRaw(p,"sn","");
		cid = PropertiesConstants.getString(p, "cid", cid,true);
	}
	
	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		CertificateStore store = CertificateStoreFactory.getDefault();
		Stack<CertificateContent> stack = ctx.getObject(cid);
		if (stack == null){
			stack = new Stack<CertificateContent>();
			CertificateContent pem = store.getRoot(new PemCertificateContent());
			stack.add(pem);

			CertificateContent issuer = stack.peek();
			long sn = PropertiesConstants.transform(ctx, $sn, 0);
			if (sn <= 0) {
				sn = System.currentTimeMillis() * 10000 + Integer.parseInt(KeyGen.uuid(5, 0, 9));
			}

			CertificateContent newPem = new PemCertificateContent();
			String cn = PropertiesConstants.transform(ctx, $cn, "");
			if (StringUtils.isNotEmpty(cn)) {
				newPem = store.newCertificate(
						BigInteger.valueOf(sn),
						newPem,
						issuer,
						String.format("CN=%s",cn));
			} else {
				newPem = store.newCertificate(
						BigInteger.valueOf(sn),
						newPem,
						issuer,
						null,
						ctx);
			}

			try {
				ctx.setObject(cid,stack);
				stack.add(newPem);
				super.onExecute(root, current, ctx, watcher);
			} finally {
				stack.pop();
				ctx.removeObject(cid);
			}
		}else {
			CertificateContent issuer = stack.peek();
			long sn = PropertiesConstants.transform(ctx, $sn, 0);
			if (sn <= 0) {
				sn = System.currentTimeMillis() * 1000000L + System.nanoTime() % 1000000L;
			}

			CertificateContent newPem = new PemCertificateContent();
			String cn = PropertiesConstants.transform(ctx, $cn, "");
			if (StringUtils.isNotEmpty(cn)) {
				newPem = store.newCertificate(
						BigInteger.valueOf(sn),
						newPem,
						issuer,
						String.format("CN=%s",cn));
			} else {
				newPem = store.newCertificate(
						BigInteger.valueOf(sn),
						newPem,
						issuer,
						null,
						ctx);
			}

			try {
				stack.add(newPem);
				super.onExecute(root, current, ctx, watcher);
			} finally {
				stack.pop();
			}
		}
	}
}
